<?

session_start();

include_once("../../Misc.php");
include_once("../../classCol/UID.php");
include_once("../../classCol/classOracleDb.php");
include_once("../models/fetchers.php");
include_once("../models/handlers.php");

include_once("../../models/GFetchers.php");
include_once("../../models/GHandlers.php");
include_once("../../helpers/CMonthlyUpdate.php");

$srcDbMstSequence = "MST_SEQUENCE";

$COracle = new oracleDb;
$COracle->init($_SESSION["ORCL"],
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);

if ($COracle->connect()) {

  $CMonthly = new CMonthlyUpdate($COracle);

  /**
  * Check if current periode isChange
  *
  */
  $cmpDate  = $_SESSION["sysPeriode"];

  if($_POST["thisPeriode"] != $cmpDate){

    $isChange = true;
  }else{

    $isChange = false;
  }

  /**
  * if isChange
  * YES: Check if the periode is greater, equal or lower than current
  * NO : Do nothing
  *
  */
  if($isChange){

    /**
    * Greater -> check if the periode is exists
    *            YES: do nothing       --> This procedure
    *            NO : Create new entry --> is done by CMonthlyUpdate::resetSequence(<periode>);
    *
    * Equal   -> init and return thisPeriode
    *
    * Lower   -> init and return thisPeriode
    *
    */
    if($_POST["thisPeriode"] >  $cmpDate){

      $CMonthly->resetSequence($_POST["thisPeriode"]);

      $_SESSION["sysPeriode"] = $_POST["thisPeriode"];

      echo  $_POST["thisPeriode"];
    }else
    if($_POST["thisPeriode"] == $cmpDate){

      $_SESSION["sysPeriode"] = $_POST["thisPeriode"];

      echo  $_POST["thisPeriode"];
    }else
    if($_POST["thisPeriode"] <  $cmpDate){

      $_SESSION["sysPeriode"] = $_POST["thisPeriode"];
      echo  $_POST["thisPeriode"];
    }else{

      echo  $_SESSION["sysPeriode"];
    }
  }else{

    echo  $_SESSION["sysPeriode"];
  }
}else{

  echo "*****";
}

$COracle->commit();
$COracle->dc();

?>